package utils.animate
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	
	import global.GameState;
	
	import utils.EnterFrameTimer;

	public class TweenEmote extends EventDispatcher
	{
		private var timer:EnterFrameTimer;
		private var _target:DisplayObject;
		private var fx:int;
		private var fy:int;
		private var tx:int;
		private var ty:int;
		private var tweenLight:TweenLight;
		private var _duration:int;
		private var _increment:int;
		
		public function TweenEmote()
		{
			initTimer();
			initTween();
		}
		
		public function set duration(v:Number):void
		{
			_duration = v * 25;
		}
		
		public function get target():DisplayObject
		{
			return _target;
		}
		
		private function initTween():void
		{
			tweenLight = new TweenLight();
			tweenLight.duration = 1;
		}
		
		private function initTimer():void
		{
			timer = new EnterFrameTimer(40);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			var speed:Number = (1 - _target.scaleX)/8;
			_target.scaleX = _target.scaleY +=speed; 
			if (_target.scaleX >= .98)
			{
				_target.scaleX = _target.scaleY = 1;
			}
			
			_increment ++;
			if (_increment > _duration)
			{
				stop();
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		public function setTarget(target:DisplayObject,sitId:int):void
		{
			this._target = target;
			this._target.scaleX = target.scaleY = .2;
			fx = GameState.sitXYS[sitId][0] + 40;
			fy = GameState.sitXYS[sitId][1] + 40;
			
			tx = getTXY(sitId).x;
			ty = getTXY(sitId).y;
			
			tweenLight.setTarget(target,fx,fy,tx,ty);
			
		}
		
		public function start():void
		{
			tweenLight.start();
			timer.start();
			_increment = 0;
		}
		
		public function stop():void
		{
			tweenLight.stop();
			timer.reset();
			
		}
		
		private function getFXY(sitId:int):Point
		{
			var p:Point = new Point();
			switch (sitId)
			{
				case 0:
					
					break;
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				case 5:
					break;
				case 6:
					break;
			}
			return p;
		}
		
		private function getTXY(sitId:int):Point
		{
			var p:Point = new Point();
			switch(sitId)
			{
				case 0:
					p.x = fx + 40;
					p.y = fy + 30;
					break;
				case 1:
					p.x = fx + 60;
					p.y = fy - 25;
					break;
				case 2:
					p.x = fx + 40;
					p.y = fy - 30;
					break;
				case 3:
					p.y = fy - 30;
					p.x = fx;
					break;
				case 4:
					p.x = fx - 40;
					p.y = fy;
					break;
				case 5:
					p.x = fx - 30;
					p.y = fy + 30;
					break;
			}
			return p;
		}
	}
}